package cn.newtouch._02_xhr;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {

    public static void main(String[] args) throws Exception {
        //1.获取生产者对象
        DefaultMQProducer producer = new DefaultMQProducer("xhr_producer_group");
        //2.设置namesever地址
        producer.setNamesrvAddr("localhost:9876");
        //3.启动
        producer.start();
        //4.生成消息并发送
        Message message = new Message("xhr", "hello", "天天写bug!!好开心！！".getBytes(StandardCharsets.UTF_8));
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("消费消息成功了！"+sendResult.toString());
            }

            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("消费消息失败了！");
            }
        });
        System.out.println("先打印这个说明是异步！！");
        //5.关闭
        //producer.shutdown();
    }
}
